package icasue.tools.sqlhelper.core;


import icasue.reflect.annotations.Nullable;
import icasue.reflect.handles.predicate.SureOF;
import icasue.tools.sqlhelper.common.Const;
import icasue.reflect.annotations.NotNull;

import java.time.temporal.TemporalAccessor;
import java.util.Date;

import static icasue.tools.sqlhelper.common.Const.*;

abstract class SqlBuild{

    // [java.util.Data] | [java.time.temporal.TemporalAccessor]
    // format to "yyyy-MM-dd HH:mm:ss".
    final static Object _formatVal(@Nullable Object val){
        if(val == null){
            return val;
        }else if(val instanceof Date){
            Date time = Date.class.cast(val);
            int year = time.getYear() + 1900;
            int month = time.getMonth() + 1;
            int day = time.getDate();
            int hours = time.getHours();
            int minutes = time.getMinutes();
            int seconds = time.getSeconds();
            return String.format(DATE_FORMAT, year, month, day, hours, minutes, seconds);
        }else if(val instanceof TemporalAccessor){
            return DF.format(TemporalAccessor.class.cast(val));
        }else if(val instanceof String){
            return String.valueOf(val).replaceAll($SEC, $DOU_SEC);
        }else {
            return val;
        }
    }

    // '%val%'
    final static String _likeItem(@NotNull Object likeVal){
        return new StringBuilder()
                .append($SEC).append($PERCENT)
                .append(likeVal)
                .append($PERCENT).append($SEC)
                .toString();
    }

    // 'val'
    final static String _eqItem(@NotNull Object eqVal){
        return new StringBuilder()
                .append($SEC)
                .append(eqVal)
                .append($SEC)
                .toString();
    }

    // ('val1',null,'val2'..)
    final static String _range(@NotNull Object... rangeValues){
        StringBuilder builder = new StringBuilder()
                .append(Const.$BRACKETS_SMALL_LEFT);
        for (Object inValue : rangeValues) {
            builder.append(_eqItem(_formatVal(inValue))).append(Const.$DOT);
        }
        if(rangeValues.length > 0){
            builder.replace(builder.length() - 1, builder.length(), Const.$EMPTY);
        }
        return builder.append(Const.$BRACKETS_SMALL_RIGHT)
                .toString();
    }


    // between 'va1' and 'val2'  |  >= 'left'  |  < 'right'  |  ""
    final static String _between(@NotNull Object left, @NotNull Object right){
        //SureOF.notNull(leftRight,"SQL segment [between] required range left and right not null.");
        //SureOF.equals(leftRight.length == 2,Boolean.TRUE, "SQL segment [between] required 2 range values, but actually getting : " + leftRight.length);
        SureOF.notNullAll(new Object[]{left,right}, "SQL segment [between] required range left and right must be not NULL.");

        return new StringBuilder()
                .append(_eqItem(left))
                .append(Const.$SPACE).append(Const.AND).append(Const.$SPACE)
                .append(_eqItem(right))
                .toString();
    }

}